<?php

class M_users extends Model{
		function M_users(){
			parent::Model();	
		}
		
		function register(){
			$admin_id = 2;
			if($this->session->userdata('id') == 1)
			$admin_id = $this->input->post('role');
			
			$query_str = array(
					'user_fullName' => $this->input->post('fullname'),
					'user_gender' => $this->input->post('gender'),
					'user_phone' => $this->input->post('phone'),
					'user_address' => $this->input->post('address'),
					'user_email' => $this->input->post('email'),
					'user_username' => $this->input->post('username'),
					'user_password' => md5($this->input->post('pass')),
					'user_role_id' => $admin_id,
					'user_statu_id' => $admin_id
					);
			return $this->db->insert('tbl_user',$query_str);
		
		}
		
		// fun check the username exists in database.........
		function checkexist($username){
				$qu = "SELECT user_username FROM tbl_user WHERE user_username = ? ";
				$result = $this->db->query($qu,$username);
			if($result->num_rows() > 0){
				return true;
			}else{
				return false;
			}
		}
		
		//function select to login .......
		function getAlluser($username,$password){		
			$this->db->select('*');
			$this->db->from('tbl_user');// select * from users
			$this->db->join('tbl_role','tbl_user.user_role_id = tbl_role.role_id');
			$this->db->where('tbl_user.user_username',$username);//where username=$username
			$this->db->where('tbl_user.user_password',$password);//where password = $password
			
			return $this->db->get();
		}
		// select account of users login----
		function sel_account(){
			$this->db->select('*');
			$this->db->from('tbl_user');// select * from users
			$this->db->join('tbl_role','tbl_user.user_role_id = tbl_role.role_id');
			$getId = $this->session->userdata('id');
			//die($getId);
			$this->db->where('tbl_user.user_id',$getId);
			
			return $this->db->get();
		}
		//===================select data to edit====================//
    
		function selet_edit_member() {
			$id = $this->uri->segment('3');
			
			$this->db->select('*');
			$this->db->from('tbl_user');
			$this->db->join('tbl_role','tbl_user.user_role_id = tbl_role.role_id');
			$this->db->where('user_id', $id);
			
			return $this->db->get();
		}
		//update account of users login by kimsong.kg@gmail.com. in admin  pages.
		function admin_update_account(){
			
			$data = array(  
				 'user_fullName' => $this->input->post("fullname"),
				 'user_gender' => $this->input->post("gender"),
				 'user_phone' => $this->input->post("phone"),
				 'user_address' => $this->input->post("address"),
				 'user_username' => $this->input->post("username"),
				 'user_role_id' => $this->input->post("role")
						 );
			$session_login = $this->input->post('getid');
			$this->db->where('user_id',$session_login);
			return $this->db->update('tbl_user',$data);	
		}
		//change your password.....by kimsong.san
		//===========fun use to delete members website.....=========
		function admin_delete_member($data){
			
			$this->db->delete('tbl_user', $data); 
			
		}
		//===========end fun delete members ========
    //=======================function used for checkedit data======================//
		//update account of users login by kimsong.kg@gmail.com..
		function update_account(){
			$session_login = $this->session->userdata('id');
			
			$data = array(
				 'user_fullName' => $this->input->post("fullname"),
				 'user_gender' => $this->input->post("gender"),
				 'user_phone' => $this->input->post("phone"),
				 'user_address' => $this->input->post("address"),
				 'user_username' => $this->input->post("username")
						 );
			$this->db->where('user_id',$session_login);
			return $this->db->update('tbl_user',$data);	
		}
		//change your password.....by kimsong.san
		function udpate_pass(){
			$session_login = $this->session->userdata('id');
			$data = array(
				 'user_password' => md5($this->input->post("pass"))
						 );
			$this->db->where('user_id',$session_login);
			
			return $this->db->update('tbl_user',$data);	
			
		}
		// fun use to show user action login........
		function view_account($limit, $start){
			$this->db->limit($limit, $start);
			$this->db->select('*');
			$this->db->from('tbl_user');
			$this->db->join('tbl_role','tbl_user.user_role_id = tbl_role.role_id');
			
			//$session_login = $this->session->userdata('id');
			$this->db->where('tbl_user.user_role_id',2);
			
			return $this->db->get();
		}
		// fun use to show user action login........
		//set status member
		function mem_statu(){
		$setsta = $this->uri->segment(3);
		$getm_id = $this->uri->segment(4);
		if($setsta == 1){
			 $data = array(
					'user_statu_id' => '0' 
					);
			 $this->db->where('user_id',$getm_id );
			return $this->db->update('tbl_user',$data);
			
			}else{
				$data = array(
					'user_statu_id' => '1'
					);
				$this->db->where('pro_id',$getpro_id);
			return $this->db->update('tbl_user',$data);
				}
		}
		
		
	}